package com.mobi.Survey.lib.activities;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobi.Survey.lib.R;
import com.mobi.Survey.lib.model.SurveyPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomAdapterSurveys.java
 * <p>
 * This class is sub class of BaseAdapter.java and a standard class. Responsible
 * for creating rows of surveys SurveyListScreen.java uses it
 * 
 */
public class CustomAdapterSurveys extends AbstractAdapterSurveyList implements View.OnClickListener

{

	public CustomAdapterSurveys(SurveyListScreen surveyScreen) {

		this.surveyScreen = surveyScreen;
		this.mInflater = LayoutInflater.from(surveyScreen);

	}

	@Override
	public int getCount() {
		if (null == surveys)
			return 0;
		return surveys.size();
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	static final class ViewHolder {

		TextView btnStartSurvey;
		TextView btnSurveyInfo;
		TextView btnAnswer;
		TextView btnSurveyResult;
		TextView btnTxtTimer;

//		ImageView ivInfo;
//		ImageView ivAnswer;
//		ImageView ivGraph;
		ImageView ivStart;
//		ImageView ivTimer;

		View startSurveyLayout;
		View graphLayout;
		View infoLayout;
		View answerLayout;
		View timerLayout;

	}

	private void setupDetailIcons(SurveyPojo survey, ViewHolder holder) {

		if (survey.getTxtButtonTimer() != null) {
			holder.timerLayout.setVisibility(View.VISIBLE);
			holder.btnTxtTimer.setText(' ' + survey.getTxtButtonTimer());
		} else {
			holder.timerLayout.setVisibility(View.GONE);
		}
		// /////////////////////////////////////////////
		if (survey.isAnswered() && null != survey.getTxtBtnCorrectAnswer()) {

			holder.answerLayout.setVisibility(View.VISIBLE);
			holder.btnAnswer.setText(' ' + survey.getTxtBtnCorrectAnswer());

//			holder.ivAnswer.setImageDrawable(surveyScreen.getResources()
//					.getDrawable(R.drawable.rmma_correct_icon_72x72));

		} else {
			holder.answerLayout.setVisibility(View.GONE);

		}

		// ///////////////////////////////////////////
		if (null != survey.getTxtBtnSurveyResult()) {
			holder.graphLayout.setVisibility(View.VISIBLE);
			holder.btnSurveyResult.setText(' ' + survey.getTxtBtnSurveyResult());

//			holder.ivGraph.setImageDrawable(surveyScreen.getResources().getDrawable(
//					R.drawable.rmma_bar_graph_icon_72x72));

		} else {
			holder.graphLayout.setVisibility(View.GONE);
		}
		// ///////////////////////////////////////////
		if (null != survey.getSubscription() && !survey.getSubscription().isNull()) {
			holder.infoLayout.setVisibility(View.VISIBLE);

			//holder.ivInfo.setImageDrawable(surveyScreen.getResources().getDrawable(R.drawable.rmma_info_icon_72x72));

		} else {

			holder.infoLayout.setVisibility(View.GONE);
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final SurveyPojo survey = surveys.get(position);
		final ViewHolder holder;
		if (convertView == null) {

			convertView = mInflater.inflate(getLayoutId(), null);
			// convertView = mInflater.inflate(LayoutIds.SURVEY_ROW_MEDIUM_1,
			// null);

			holder = new ViewHolder();

			// //////////////////////////////
			holder.btnStartSurvey = (TextView) convertView.findViewById(R.id.btnStartSurvey);

			holder.btnSurveyInfo = (TextView) convertView.findViewById(R.id.btnSurveyInfo);

			holder.btnAnswer = (TextView) convertView.findViewById(R.id.btnAnswer);

			holder.btnSurveyResult = (TextView) convertView.findViewById(R.id.btnSurveyResult);
			holder.btnTxtTimer = (TextView) convertView.findViewById(R.id.btnTxtTimer);

			// ///////////////////////////
//			holder.ivAnswer = (ImageView) convertView.findViewById(R.id.ivAnswer);
//			holder.ivInfo = (ImageView) convertView.findViewById(R.id.ivInfo);
//			holder.ivGraph = (ImageView) convertView.findViewById(R.id.ivGraph);
 			holder.ivStart = (ImageView) convertView.findViewById(R.id.ivStart);
//			holder.ivTimer = (ImageView) convertView.findViewById(R.id.ivTimer);

			// ///////////////////////////

			holder.startSurveyLayout = convertView.findViewById(R.id.startLayout);
			holder.graphLayout = convertView.findViewById(R.id.graphLayout);
			holder.infoLayout = convertView.findViewById(R.id.infoLayout);
			holder.answerLayout = convertView.findViewById(R.id.answerLayout);
			holder.timerLayout = convertView.findViewById(R.id.timerLayout);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		holder.btnStartSurvey.setText(' ' + survey.getSurveyDiscription());

		// Show or hide Option buttons
		setupDetailIcons(survey, holder);

		// //////////////////////////////////
		if (surveys.get(position).getBitmapIcon() != null) {
			try {
				holder.ivStart.setImageBitmap(surveys.get(position).getBitmapIcon());
			} catch (OutOfMemoryError e) {
				System.gc();
				Log.e(this.getClass().getSimpleName(), "Out of memory due too   app icon image");
				holder.ivStart.setImageDrawable(surveyScreen.getResources().getDrawable(R.drawable.icon));

			}

		} else {
			holder.ivStart.setImageDrawable(surveyScreen.getResources().getDrawable(R.drawable.icon));
		}

		// Listeners
		holder.startSurveyLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				surveyScreen.onClick(new SurveyClick(R.id.btnStartSurvey, survey));
			}
		});

		holder.answerLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				surveyScreen.onClick(new SurveyClick(R.id.btnAnswer, survey));
			}
		});

		holder.graphLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				surveyScreen.onClick(new SurveyClick(R.id.btnSurveyResult, survey));
			}
		});

		holder.infoLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				surveyScreen.onClick(new SurveyClick(R.id.btnSurveyInfo, survey));
			}
		});

		holder.timerLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				surveyScreen.onClick(new SurveyClick(R.id.btnTxtTimer, survey));
			}
		});

		return convertView;

	}

	/**
	 * clear all Bitmap icons of survey list resources held by Survey List
	 */
	public void dispose() {
		super.dispose();

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
